using System;
using System.IO;
using System.Windows.Forms;
internal delegate void FeeBack(int value);
public sealed class Program{
	
	public static void Main(){
		//StaticDelegateDemo();
		//InstanceDelegate();
		
		ChainDelegateDemo(new Program());
	}
	private static void StaticDelegateDemo(){
		Console.WriteLine("静态委托 小样");
		Counter(1,3,null);
		
		Counter(1,3,new FeeBack(Program.FeeBackToConsole));
		
		Counter(1,3,new FeeBack(FeeBackToMsgBox));
		Console.WriteLine();
	}
	
	private static void InstanceDelegate(){
		Console.WriteLine("Instance委托 小样");
		Program p=new Program();
		Counter(1,3,new FeeBack(p.FeeBackToFile));
		Console.WriteLine();
	}
	
	private static void ChainDelegateDemo(Program p)
	{
		Console.WriteLine("Chain委托（是不是委托连？） 小样");
		
		FeeBack fb1=new FeeBack(FeeBackToConsole);
		FeeBack fb2=new FeeBack(FeeBackToMsgBox);
		FeeBack fb3=new FeeBack(p.FeeBackToFile);
		
		FeeBack fbchain=null;
		fbchain=(FeeBack)Delegate.Combine(fbchain,fb1);
		fbchain=(FeeBack)Delegate.Combine(fbchain,fb2);
		fbchain=(FeeBack)Delegate.Combine(fbchain,fb3);
		
		Counter(1,2,fbchain);
		
		Console.WriteLine();
		
		fbchain=(FeeBack)Delegate.Remove(fbchain,new FeeBack(FeeBackToMsgBox));
		Counter(1,2,fbchain);
		
	}
	
	
	private static void Counter(int from,int to ,FeeBack fb){
		
		for	(int val=from;val<=to;val++){
			//	如果有回调函数 fb  
			if(fb!=null)fb(val);
			
		}
	}
	private static void FeeBackToConsole(int value){Console.WriteLine("Item:"+value);}
	private static void FeeBackToMsgBox(int value){MessageBox.Show("Item:"+value);}
	private void FeeBackToFile(int value)
	{
		StreamWriter sw=new StreamWriter("Status",true);
		sw.WriteLine("Item:"+value);
		sw.Close();
					
	}
}